<template>
  <div>
    <ul>
      <li v-for="group in singer" class="list-group">
        <h2 class="list-group-title">{{group.initial}}</h2>
        <ul>
          <li v-for="item in group.children" class="list-group-item">
            <img :src="item.avatar" class="avatar">
            <span class="name">{{item.name}}</span>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
import { getSingerList } from 'api/singer'
import { ERR_OK } from 'api/config'
import Singer from 'common/js/singer'
export default {
  data() {
    return {
      singer: []
    }
  },
  created() {
    this._getSingerList()
  },
  methods: {
    _getSingerList() {
      getSingerList().then((res) => {
        if (res.code === ERR_OK) {
          this.singer = this._formatSinger(res.data.list)
          console.log(this.singer)
        }
      })
    },
    _formatSinger(list) {
      let map = {}
      list.forEach((item) => {
        const key = item.Findex
        if (!map[key]) {
          map[key] = []
        }
        map[key].push(
          new Singer(item.Fsinger_mid, 
            item.Fsinger_name)
          )
      })
      let arr = []
      for (let key in map) {
        arr.push({
          initial: key,
          children: map[key]
        })
      }
      arr.sort((a, b) => {
        return a.initial.charCodeAt(0) - b.initial.charCodeAt(0)
      })
      return arr
    }
  }
}
</script>

<style lang="stylus" scoped rel="stylesheet/stylus">
  
</style>